package com.neu.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.ShelfDto;
import com.neu.wms.entity.WmsShelf;
import com.neu.wms.service.ShelfService;
import com.neu.wms.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shelf")
public class ShelfController {
    @Autowired
    ShelfService shelfService;

    @PostMapping("queryAll")
    public ResultVo selectAll(@RequestBody ShelfDto shelfDto)
    {
        PageHelper.startPage(shelfDto.getCurrentPage(),shelfDto.getPageSize(),true);
        List<WmsShelf> shelf = shelfService.selectAll(shelfDto);
        PageInfo<WmsShelf> pageInfo = new PageInfo<>(shelf);
        return ResultVo.ok().data("list",shelf).data("total",pageInfo.getTotal());
    }

    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        WmsShelf wmsShelf = shelfService.selectByPrimaryKey(id);
        return ResultVo.ok().data("shelf",wmsShelf);
    }

    @PostMapping("")
    public ResultVo add(@RequestBody @Valid WmsShelf wmsShelf)
    {
        int i = shelfService.insert(wmsShelf);
        if (i>0) {
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @PutMapping("")
    public ResultVo update(@RequestBody @Valid WmsShelf wmsShelf)
    {
        int i = shelfService.update(wmsShelf);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @PutMapping("/quit")
    public ResultVo updateDelFlag(@RequestBody ShelfDto shelfDto)
    {
        if(shelfDto.getDelFlag()==null)
            throw new IllegalArgumentException("Argument cannot be null or empty");
        int i = shelfService.updateDelFlag(shelfDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @DeleteMapping("{id}")
    public ResultVo delete(@PathVariable("id")Long id)
    {
        int i = shelfService.delete(id);
        if (i>0) {
            return ResultVo.ok("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }}
